import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:ft_reader/beans/book_item_bean.dart';
import 'package:ft_reader/blocs/book_list_bloc.dart';
import 'package:ft_reader/widgets/book_list_item.dart';
import 'package:ft_reader/widgets/loading_widget.dart';
import 'package:ft_reader/widgets/vertical_widget.dart';

class ListPage extends StatefulWidget {

  @override
  _ListPage createState() => _ListPage();

}

class _ListPage extends State<ListPage>
 with AutomaticKeepAliveClientMixin {

  List<String> _kindsTitles = ["最新发布", "本周最热", "最多收藏", "小编推荐"];

  List<String> _kinds = ["new", "hot", "collect", "commend"];

  //当前类别
  String _curKind = "new";

  //当前性别
  String _curSex = "man";

  //当前页
  int _curPage = 1;

  // 标识是否加载更多
  bool _isLoadMore = false;

  BookListBloc _bookListBloc = new BookListBloc();

  List<BookItemBean> _book = new List();

  final ScrollController _scrollController = new ScrollController();

  GlobalKey<EasyRefreshState> _easyRefreshKey = GlobalKey<EasyRefreshState>();
  GlobalKey<RefreshHeaderState> _headerKey = new GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<RefreshFooterState>();

  // 被「sync*」标记的函数，一定要返回一个 「Iterable」，这样的函数生成器叫做同步生成器
  Iterable<Widget> get kindSelects sync* {
    for (String kind in _kinds) {
      yield VerticalWidget(
        selectedColor: Theme.of(context).primaryColor,
        label: _kindsTitles[_kinds.indexOf(kind)],
        isSelect: _curKind == kind,
        onSelected: (bool value) {
          setState(() {
            if (value) {
              _isLoadMore = false;
              _curPage = 1;
              _curKind = kind;
              _bookListBloc.fetchBookList(_curSex, _curKind, _curPage);
            }
          });
        },
      );
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _isLoadMore = false;
    _bookListBloc.fetchBookList(_curSex, _curKind, _curPage);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Container(
            color: Colors.grey[200],
            width: 100,
            child: Column(
              children: kindSelects.toList(),
            ),
          ),
          Expanded(
              child: Container(
                color: Colors.white10,
                margin: EdgeInsets.only(top: 20),
                child: Column(
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.all(10),
                      width: 400,
                      child: CupertinoSegmentedControl(
                        borderColor: Colors.white10,
                        unselectedColor: Colors.grey[500],
                        selectedColor: Theme.of(context).primaryColor.withOpacity(0.8),
                        children: {
                          'man': Text(
                              '男生',
                            style: TextStyle(color: Colors.white),
                          ),
                          'lady': Text(
                            '女生',
                            style: TextStyle(color: Colors.white),
                          )
                        },
                        onValueChanged: (key) {
                          setState(() {
                            _curSex = key;
                            _isLoadMore = false;
                            _bookListBloc.fetchBookList(_curSex, _curKind, _curPage);
                          });
                        },
                        groupValue: _curSex,
                      ),
                    ),
                    StreamBuilder(
                      stream: _bookListBloc.bookListBloc,
                      builder: (BuildContext context,
                        AsyncSnapshot<List<BookItemBean>> content) {
                        if (content.hasData) {
                          if (!_isLoadMore) {
                            _book.clear();
                          }
                          _book.addAll(content.data);
                          return Expanded(
                            child: EasyRefresh(
                              key: _easyRefreshKey,
                              child: ListView.separated(
                                controller: _scrollController,
                                  itemBuilder: (BuildContext context, int index) {
                                    return bookListItem(context, index, _book[index]);
                                  },
                                  separatorBuilder: (BuildContext context, int index) {
                                    return Divider(
                                      height: 10,
                                      color: Theme.of(context).primaryColor,
                                    );
                                  },
                                  itemCount: _book.length
                              ),
                              refreshHeader: ClassicsHeader(
                                moreInfoColor: Theme.of(context).primaryColor,
                                bgColor: Colors.white10,
                                textColor: Theme.of(context).primaryColor,
                                key: _headerKey,
                                refreshText: "用力一点",
                                refreshReadyText: "释放",
                                refreshingText: "刷新中",
                                refreshedText: "刷新完成",
                                moreInfo: "上次刷新: %T",
                                showMore: true,
                              ),
                              refreshFooter: ClassicsFooter(
                                moreInfoColor: Theme.of(context).primaryColor,
                                bgColor: Colors.white10,
                                textColor: Theme.of(context).primaryColor,
                                key: _footerKey,
                                loadText: "用力一点",
                                loadReadyText: "释放",
                                loadingText: "加载中",
                                loadedText: "加载完成",
                                moreInfo: "上次加载：%T",
                                showMore: true,
                              ),
                              onRefresh: () async {
                                _isLoadMore = false;
                                _curPage = 1;
                                _bookListBloc.fetchBookList(
                                    _curSex, _curKind, _curPage);
                              },
                              loadMore: () async {
                                _isLoadMore = true;
                                _curPage++;
                                _bookListBloc.fetchBookList(
                                    _curSex, _curKind, _curPage);
                              },
                            ),
                          );
                        } else {
                          return Center(
                            child: LoadingWidget(
                                color: Theme.of(context).primaryColor
                            )
                          );
                        }
                      },
                    )
                  ],
                ),
              )
          )
        ],
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

}